package com.king.zxing.app.fragments;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.cardview.widget.CardView;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.zxing.Result;
import com.king.camera.scan.AnalyzeResult;
import com.king.camera.scan.CameraScan;
import com.king.zxing.BarcodeCameraScanActivity;
import com.king.zxing.app.FullScreenQRCodeScanActivity;
import com.king.zxing.app.GoodsDetailActivity;
import com.king.zxing.app.MultiFormatScanActivity;
import com.king.zxing.app.R;
import com.king.zxing.app.dao.GoodsDao;
import com.king.zxing.app.domain.Goods;
import com.king.zxing.app.domain.User;


import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class MallFragment extends Fragment {

    List<Goods> goodsList = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mall, container, false);
        RecyclerView recyclerView = view.findViewById(R.id.recycler_view);
        ImageView scanQRBtn = view.findViewById(R.id.scan_qr_button);
        //点击扫描二维码按钮
        scanQRBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), MultiFormatScanActivity.class);
                startActivity(intent);
            }
        });

        goodsList = new GoodsDao(getContext()).getGoodsList();

        recyclerView.setAdapter(new MyAdapter());
        GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 2);
        recyclerView.setLayoutManager(layoutManager);

        return view;
    }


    class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {

        @NonNull
        @Override
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = View.inflate(getContext(), R.layout.item_list_goods, null);
            return new MyViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
            Goods item = goodsList.get(position);
            int imageResource = Goods.askImageResource((int) item.imageID);

            User user = Objects.requireNonNull(requireActivity().getIntent().getExtras()).getParcelable("user");

            holder.image.setImageResource(imageResource);
            holder.name.setText(item.name);
            holder.cardView.setOnClickListener(v -> {
                Intent intent = new Intent();

                assert user != null;
                intent.putExtra("user_id", user.id);
                intent.putExtra("goods_id", item.id);
                intent.setClass(requireContext(), GoodsDetailActivity.class);
                startActivity(intent);
            });
            holder.price.setText(item.price);
            for (Pair<String, String> pair : item.tags) {
                if (pair.first.equals("tag1")) holder.tag1.setText(pair.second);
                if (pair.first.equals("tag2")) holder.tag2.setText(pair.second);
                if (pair.first.equals("tag3")) holder.tag3.setText(pair.second);
            }
        }

        @Override
        public int getItemCount() {
            return goodsList.size();
        }
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {

        CardView cardView;
        ImageView image;
        TextView name;
        TextView price;

        TextView tag1;
        TextView tag2;
        TextView tag3;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            cardView = itemView.findViewById(R.id.card_view);
            image = itemView.findViewById(R.id.goods_image);
            name = itemView.findViewById(R.id.goods_name);
            price = itemView.findViewById(R.id.goods_price);
            tag1 = itemView.findViewById(R.id.goods_tag1);
            tag2 = itemView.findViewById(R.id.goods_tag2);
            tag3 = itemView.findViewById(R.id.goods_tag3);
        }
    }
}
